package com.meta.context.source.http;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.meta.internal.Options;
import com.meta.context.source.IMetaSource;
import com.meta.context.source.http.util.MateConstant;
import com.meta.context.source.http.util.RequestUtil;

import javax.tools.Diagnostic;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MetaApiSourceImpl implements IMetaSource {
    @Override
    public Map<String, JSONObject> getSources(Options options) {
        options.getProcessingEnv().getMessager().printMessage(Diagnostic.Kind.NOTE, "======doing print source" + options.getSourceType());
        Map<String, JSONObject> jsObjects = new HashMap<>();
        HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("X-API-KEY", "edd1c9f034335f136f87ad84b625c8f1");
        String url = "http://localhost:9180/apisix/admin/plugins/list";
        String routesStr = RequestUtil.requestApi(url, null, MateConstant.GET, headerMap);
        List<String> list = JSON.parseArray(routesStr, String.class);
        for (String s : list) {
            url = "http://localhost:9180/apisix/admin/plugins/" + s + "?subsystem=http";
            routesStr = RequestUtil.requestApi(url, null, MateConstant.GET, headerMap);
            jsObjects.put(s, (JSONObject) JSON.parse(routesStr));
        }
        return jsObjects;
    }

}
